package catchData.notWellDone;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * @author Suwy
 *	中华英才网
 */
public class Catch4Chinahr {
	
	private static HttpClient client;
	private static final String LOGIN_PAGE = "http://jobsearch.chinahr.com/%E8%81%8C%E4%BD%8D/?cy=cn";
	private static final String HOST = "jobsearch.chinahr.com";
	
	private void initHttpClient() {
		if (client == null) {
			MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
			client = new HttpClient(connectionManager);
			// 配置服务器参量
			client.getParams().setCookiePolicy(
					CookiePolicy.BROWSER_COMPATIBILITY); // 配置Cookie策略为浏览器默认策略
			client.getParams().setParameter(
					"http.protocol.single-cookie-header", true);
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "gb2312");
			client.getHttpConnectionManager().getParams().setConnectionTimeout(300000);
			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
		}
	}
	
	private void setHeaders(HttpMethod method) {
		method.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
		method.setRequestHeader("accept","text/html,application/xhtml+xml,application/xml,image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		method.setRequestHeader("referer", LOGIN_PAGE);
		method.setRequestHeader("accept-encoding", "GBK,UTF-8,GB2312,gzip,deflate,sdch");
		method.setRequestHeader("accept-language", "zh-CN");
		method.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		method.setRequestHeader("user-agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
		method.setRequestHeader("host", HOST);
		method.setRequestHeader("connection", "Keep-Alive");
		method.setRequestHeader("cache-control", "no-cache");
		method.setRequestHeader("Cookie","_e_refPartner=false; ASP.NET_SessionId=lfqku2pkktikcfeinlpgxptx; split_scsjsv=35; wtspc=5; 5515_JSRadius=30; SortBy=rv.dt.di; jsLucCrit=cy=cn; jsCrit=cy%3dcn%26ispowersearch%3d0; scsjsv=0; JSRTimeStamp=634928946188372934; NumberOfJSR=2; uslc=1; refU=http%3A//jobsearch.chinahr.com/%25E8%2581%258C%25E4%25BD%258D/%3Fcy%3Dcn; OAS_SC1=1357269038233; WT_FPC=id=10.5.199.241-2401076160.30272040:lv=1357222238806:ss=1357222038772; batchApplyInitiated=1");
	}
	
	public String getNow() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	private void saveDataFile(String sFolder, String fileName, String data, int isidd) {
		File folder = new File(sFolder+"/"+fileName);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = folder.toString() + "/" + isidd + ".htm";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(saveFile), "utf-8"));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("【" + getNow() + "】 保存文件成功！"+ saveFile.replaceAll("\\\\", "/"));
		}
	}
	
	public static void main(String[] args) {
		try {
			Catch4Chinahr instance = new Catch4Chinahr();
			String job = "java";
			int start = 1;
			int end = 1;
			String pathURL = "http://jobsearch.chinahr.com/%E8%81%8C%E4%BD%8D/%E5%B9%BF%E4%B8%9C%E7%9C%81+%E5%B9%BF%E5%B7%9E%E5%B8%82_12?q="+job+"&pg=";
			PostMethod method = null;
			instance.initHttpClient();
			for(int i = start; i<= end; i++){
				method = new PostMethod(pathURL+i+"&where=%E5%B9%BF%E4%B8%9C%E7%9C%81%E5%B9%BF%E5%B7%9E%E5%B8%82&rad=30-km&cy=cn");
				instance.setHeaders(method);
				int a = client.executeMethod(method);
				String str = method.getResponseBodyAsString();
				System.out.println(str);
				method.releaseConnection();
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}